//
//  LBBasicChoiceCityView.m
//  app
//
//  Created by 杨庆人 on 16/12/26.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBBasicChoiceCityView.h"
#import <Masonry/Masonry.h>
#import <LBCategory/LBCategory.h>

@interface LBBasicChoiceCityView ()

@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * chatStr;
@property (nonatomic,assign)LBCityPickerViewType cityPickerViewType;

@property (nonatomic,strong)LBMultiLevCityView * multiLevCityView;

@end
@implementation LBBasicChoiceCityView

- (instancetype)initWithTitle:(NSString *)title chatStr:(NSString *)chatStr cityPickerViewType:(LBCityPickerViewType)cityPickerViewType{
    if (self = [super init]) {
        _title = title;
        _chatStr = chatStr;
        _cityPickerViewType = cityPickerViewType;
        [self setMultiLevCityView];
    }
    return self;
}
- (LBMultiLevCityView *)multiLevCityView {
    if (!_multiLevCityView) {
        __weak typeof(self)weakSelf = self;
        _multiLevCityView = [[LBMultiLevCityView alloc] initWithCityPickerViewType:_cityPickerViewType cityChoseBlock:^(LBCityMsgModel *selectedProvinceModel, LBCityMsgModel *selectedCityModel, LBCityMsgModel *selectedAreaModel) {
            
            weakSelf.provinceName = selectedProvinceModel.areaName;
            weakSelf.provinceCode = selectedProvinceModel.areaCode;
            weakSelf.cityName = selectedCityModel.areaName;
            weakSelf.cityCode = selectedCityModel.areaCode;
            weakSelf.areaName = selectedAreaModel.areaName;
            weakSelf.areaCode = selectedAreaModel.areaCode;
            
            !weakSelf.ChoiceCityBlock ? :weakSelf.ChoiceCityBlock(selectedProvinceModel,selectedCityModel,selectedAreaModel);
        }];
    }
    return _multiLevCityView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.textColor = [UIColor colorWithR:134 G:134 B:134 A:1];
    }
    return _titleLabel;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo(17);
    }];
    [self.multiLevCityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(25);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(8);
    }];
    
}
- (void)setMultiLevCityView {
    [self addSubview:self.titleLabel];
    [self addSubview:self.multiLevCityView];
    
    if (_chatStr.length) {
        NSString * string = [NSString stringWithFormat:@"%@%@",_title,_chatStr];
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:string];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:74/255.0 green:144/255.0 blue:226/255.0 alpha:1] range:[string rangeOfString:_chatStr]];
        self.titleLabel.attributedText = attrStr;
    }
    else {
        self.titleLabel.text = _title;
    }
}
- (void)setDefaultCityMsgWithProvinceName:(NSString *)provinceName
                                 ProvinceCode:(NSString *)provinceCode
                                 cityName:(NSString *)cityName
                                 cityCode:(NSString *)cityCode
                                 areaName:(NSString *)areaName
                                 areaCode:(NSString *)areaCode {
    self.provinceName = provinceName;
    self.cityName = cityName;
    self.areaName = areaName;
    self.provinceCode = provinceCode;
    self.cityCode = cityCode;
    self.areaCode = areaCode;
    [self.multiLevCityView.provBtn setTitle:provinceName forState:UIControlStateNormal];
    [self.multiLevCityView.cityBtn setTitle:cityName forState:UIControlStateNormal];
    [self.multiLevCityView.areaBtn setTitle:areaName forState:UIControlStateNormal];
}

@end
